home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktimeintro / play movie w.controller / application files / comapplication.c next >
Encoding:
Text File  |  2000-10-06  |  14.0 KB  |  532 lines

  1. //////////
  2. //
  3. //    File:        ComApplication.c
  4. //
  5. //    Contains:    Application-specific code for basic QuickTime movie display and control.
  6. //
  7. //    Written by:    Tim Monroe
  8. //                Based on the QTShell code written by Tim Monroe, which in turn was based on the MovieShell
  9. //                code written by Kent Sandvik (Apple DTS). This current version is now very far removed from
  10. //                MovieShell.
  11. //
  12. //    Copyright:    © 1999 by Apple Computer, Inc., all rights reserved.
  13. //
  14. //    Change History (most recent first):
  15. //       
  16. //       <2>         03/02/00    rtm        made changes to get things running under CarbonLib
  17. //       <1>         11/05/99    rtm        first file
  18. //
  19. //////////
  20.  
  21. //////////
  22. //
  23. // header files
  24. //
  25. //////////
  26.  
  27. #include "ComApplication.h"
  28.  
  29.  
  30. //////////
  31. //
  32. // global variables
  33. //
  34. //////////
  35.  
  36. #if TARGET_OS_MAC
  37. AEEventHandlerUPP        gHandleOpenAppAEUPP;                    // UPPs for our Apple event handlers
  38. AEEventHandlerUPP        gHandleOpenDocAEUPP;
  39. AEEventHandlerUPP        gHandlePrintDocAEUPP;
  40. AEEventHandlerUPP        gHandleQuitAppAEUPP;
  41. extern Boolean            gAppInForeground;                        // is our application in the foreground?    
  42. #endif
  43.  
  44. #if TARGET_OS_WIN32
  45. extern HWND                ghWnd;                                    // the MDI frame window; this window has the menu bar
  46. #endif
  47.  
  48.  
  49. //////////
  50. //
  51. // QTApp_Init
  52. // Do any application-specific initialization.
  53. //
  54. // The theStartPhase parameter determines which "phase" of application start-up is executed,
  55. // *before* the MDI frame window is created or *after*. This distinction is relevant only on
  56. // Windows, so on MacOS, you should always use kInitAppPhase_BothPhases.
  57. //
  58. //////////
  59.  
  60. void QTApp_Init (UInt32 theStartPhase)
  61. {
  62.     // do any start-up activities that should occur before the MDI frame window is created
  63.     if (theStartPhase & kInitAppPhase_BeforeCreateFrameWindow) {
  64. #if TARGET_OS_MAC
  65.         // make sure that the Apple Event Manager is available; install handlers for required Apple events
  66.         QTApp_InstallAppleEventHandlers();
  67. #endif
  68.     }
  69.  
  70.     // do any start-up activities that should occur after the MDI frame window is created
  71.     if (theStartPhase & kInitAppPhase_AfterCreateFrameWindow) {
  72. #if TARGET_OS_WIN32
  73.         // on Windows, open as movie documents any files specified on the command line
  74.         SendMessage(ghWnd, WM_OPENDROPPEDFILES, 0L, 0L);
  75. #endif
  76.     }
  77. }
  78.  
  79.  
  80. //////////
  81. //
  82. // QTApp_Stop
  83. // Do any application-specific shut-down.
  84. //
  85. // The theStopPhase parameter determines which "phase" of application shut-down is executed,
  86. // *before* any open movie windows are destroyed or *after*.
  87. //
  88. //////////
  89.  
  90. void QTApp_Stop (UInt32 theStopPhase)
  91. {    
  92.     // do any shut-down activities that should occur before the movie windows are destroyed
  93.     if (theStopPhase & kStopAppPhase_BeforeDestroyWindows) {
  94.         
  95.     }
  96.     
  97.     // do any shut-down activities that should occur after the movie windows are destroyed
  98.     if (theStopPhase & kStopAppPhase_AfterDestroyWindows) {
  99. #if TARGET_OS_MAC
  100.         // dispose of routine descriptors for Apple event handlers
  101.         DisposeAEEventHandlerUPP(gHandleOpenAppAEUPP);
  102.         DisposeAEEventHandlerUPP(gHandleOpenDocAEUPP);
  103.         DisposeAEEventHandlerUPP(gHandlePrintDocAEUPP);
  104.         DisposeAEEventHandlerUPP(gHandleQuitAppAEUPP);
  105. #endif
  106.     
  107.     }
  108. }
  109.  
  110.  
  111. //////////
  112. //
  113. // QTApp_Idle
  114. // Do any processing that can/should occur at idle time.
  115. //
  116. //////////
  117.  
  118. void QTApp_Idle (WindowReference theWindow)
  119. {
  120.     WindowObject         myWindowObject = NULL;
  121.     GrafPtr             mySavedPort;
  122.     
  123.     GetPort(&mySavedPort);
  124.     MacSetPort(QTFrame_GetPortFromWindowReference(theWindow));
  125.     
  126.     myWindowObject = QTFrame_GetWindowObjectFromWindow(theWindow);
  127.     if (myWindowObject != NULL) {
  128.         MovieController        myMC = NULL;
  129.     
  130.         myMC = (**myWindowObject).fController;
  131.         if (myMC != NULL) {
  132.             
  133.             // run any idle-time tasks for the movie
  134.             
  135. #if TARGET_OS_MAC
  136.             // restore the cursor to the arrow
  137.             // if it's outside the front movie window or outside the window's visible region
  138.             if (theWindow == QTFrame_GetFrontMovieWindow()) {
  139.                 Rect            myRect;
  140.                 Point            myPoint;
  141.                 RgnHandle        myVisRegion;
  142.                 Cursor            myArrow;
  143.                 
  144.                 GetMouse(&myPoint);
  145.                 myVisRegion = NewRgn();
  146.                 GetPortVisibleRegion(QTFrame_GetPortFromWindowReference(theWindow), myVisRegion);
  147.                 GetWindowPortBounds(theWindow, &myRect);
  148.                 if (!MacPtInRect(myPoint, &myRect) || !PtInRgn(myPoint, myVisRegion))
  149.                     MacSetCursor(GetQDGlobalsArrow(&myArrow));
  150.                     
  151.                 DisposeRgn(myVisRegion);
  152.  
  153.             }
  154. #endif // TARGET_OS_MAC
  155.         }
  156.     }
  157.     
  158.     // ***insert application-specific idle-time processing here***
  159.     
  160.     MacSetPort(mySavedPort);
  161. }
  162.  
  163.  
  164. //////////
  165. //
  166. // QTApp_HandleContentClick
  167. // Handle mouse button clicks in the specified window.
  168. //
  169. //////////
  170.  
  171. void QTApp_HandleContentClick (WindowReference theWindow, EventRecord *theEvent)
  172. {
  173. #pragma unused(theEvent)
  174.  
  175.     GrafPtr             mySavedPort;
  176.     
  177.     GetPort(&mySavedPort);
  178.     MacSetPort(QTFrame_GetPortFromWindowReference(theWindow));
  179.     
  180.     // ***insert application-specific content-click processing here***
  181.  
  182.     MacSetPort(mySavedPort);
  183. }
  184.  
  185.  
  186. //////////
  187. //
  188. // QTApp_HandleKeyPress
  189. // Handle application-specific key presses.
  190. // Returns true if the key press was handled, false otherwise.
  191. //
  192. //////////
  193.  
  194. Boolean QTApp_HandleKeyPress (char theCharCode)
  195. {
  196.     Boolean        isHandled = true;
  197.     
  198.     switch (theCharCode) {
  199.     
  200.         // ***insert application-specific key-press processing here***
  201.  
  202.         default:
  203.             isHandled = false;
  204.             break;
  205.     }
  206.  
  207.     return(isHandled);
  208. }
  209.  
  210.  
  211. //////////
  212. //
  213. // QTApp_HandleMenu
  214. // Handle selections in the application's menus.
  215. //
  216. // The theMenuItem parameter is a UInt16 version of the Windows "menu item identifier". 
  217. // When called from Windows, theMenuItem is simply the menu item identifier passed to the window procedure.
  218. // When called from MacOS, theMenuItem is constructed like this:
  219. //     *high-order 8 bits == the Macintosh menu ID (1 thru 256)
  220. //     *low-order 8 bits == the Macintosh menu item (sequential from 1 to ordinal of last menu item in menu)
  221. // In this way, we can simplify the menu-handling code. There are, however, some limitations, mainly that
  222. // the menu item identifiers on Windows must be derived from the Mac values. 
  223. //
  224. //////////
  225.  
  226. Boolean QTApp_HandleMenu (UInt16 theMenuItem)
  227. {
  228.     WindowObject        myWindowObject = NULL;
  229.     MovieController     myMC = NULL;
  230.     Movie                 myMovie = NULL;
  231.     Boolean                myIsHandled = false;            // false => allow caller to process the menu item
  232.     
  233.     myWindowObject = QTFrame_GetWindowObjectFromFrontWindow();
  234.     if (myWindowObject != NULL) {
  235.         myMC = (**myWindowObject).fController;
  236.         myMovie = (**myWindowObject).fMovie;
  237.     }
  238.     
  239.     // make sure we have a valid movie controller
  240.     if (myMC == NULL)
  241.         return(myIsHandled);
  242.  
  243.     switch (theMenuItem) {
  244.     
  245.         case IDM_CONTROLLER:
  246.             QTUtils_ToggleControllerBar(myMC);
  247.             myIsHandled = true;
  248.             break;
  249.  
  250.         case IDM_SPEAKER_BUTTON:
  251.             QTUtils_ToggleControllerButton(myMC, kQTUtilsSpeakerButton);
  252.             myIsHandled = true;
  253.             break;
  254.             
  255.         default:
  256.             break;
  257.             
  258.     } // switch (theMenuItem)
  259.     
  260.     return(myIsHandled);
  261. }
  262.  
  263.  
  264. //////////
  265. //
  266. // QTApp_AdjustMenus
  267. // Adjust state of items in the application's menus.
  268. //
  269. // Currently, the Mac application has only one app-specific menu ("Test"); you could change that.
  270. //
  271. //////////
  272.  
  273. void QTApp_AdjustMenus (WindowReference theWindow, MenuReference theMenu)
  274. {
  275. #if TARGET_OS_MAC
  276. #pragma unused(theMenu)
  277. #endif
  278.  
  279.     WindowObject        myWindowObject = NULL; 
  280.     MovieController     myMC = NULL;
  281.     MenuReference        myMenu;
  282.     
  283. #if TARGET_OS_WIN32
  284.     myMenu = theMenu;
  285. #endif
  286. #if TARGET_OS_MAC
  287.     myMenu = GetMenuHandle(kTestMenuResID);
  288. #endif
  289.     
  290.     if (theWindow != NULL)
  291.         myWindowObject = QTFrame_GetWindowObjectFromWindow(theWindow);
  292.  
  293.     if (myWindowObject != NULL)
  294.         myMC = (**myWindowObject).fController;
  295.  
  296.     // ***insert application-specific menu adjusting here***
  297.  
  298.     if (myMC == NULL) {
  299.         // no movie controller, so disable all the Test menu items
  300.         QTFrame_SetMenuItemState(myMenu, IDM_CONTROLLER, kDisableMenuItem);
  301.         QTFrame_SetMenuItemState(myMenu, IDM_SPEAKER_BUTTON, kDisableMenuItem);
  302.     } else {
  303.         // we've got a movie controller, so it's safe to proceed....
  304.         QTFrame_SetMenuItemState(myMenu, IDM_CONTROLLER, kEnableMenuItem);
  305.  
  306.         // if controller bar is visible, enable Test menu items
  307.         if (QTUtils_IsControllerBarVisible(myMC)) {
  308.             QTFrame_SetMenuItemLabel(myMenu, IDM_CONTROLLER, "Hide &Controller");
  309.                // ungray the other menu items
  310.             QTFrame_SetMenuItemState(myMenu, IDM_SPEAKER_BUTTON, kEnableMenuItem);
  311.             
  312.             // handle Test menu items
  313.             if (QTUtils_IsControllerButtonVisible(myMC, kQTVRSpeakerButton))
  314.                 QTFrame_SetMenuItemLabel(myMenu, IDM_SPEAKER_BUTTON, "Hide Spea&ker Button");
  315.             else 
  316.                 QTFrame_SetMenuItemLabel(myMenu, IDM_SPEAKER_BUTTON, "Show Spea&ker Button");
  317.             
  318.         } else {
  319.             // controller bar is not visible
  320.             
  321.             QTFrame_SetMenuItemLabel(myMenu, IDM_CONTROLLER, "Show &Controller");
  322.               // gray the other menu items
  323.             QTFrame_SetMenuItemState(myMenu, IDM_SPEAKER_BUTTON, kDisableMenuItem);
  324.         }
  325.     }
  326. }
  327.  
  328.  
  329. //////////
  330. //
  331. // QTApp_HandleEvent
  332. // Perform any application-specific event loop actions.
  333. //
  334. // Return true to indicate that we've completely handled the event here, false otherwise.
  335. //
  336. //////////
  337.  
  338. Boolean QTApp_HandleEvent (EventRecord *theEvent)
  339. {
  340. #pragma unused(theEvent)
  341.  
  342.     // ***insert application-specific event handling here***
  343.     
  344.     return(false);            // no-op for now
  345. }
  346.  
  347. //////////
  348. //
  349. // QTApp_SetupWindowObject
  350. // Do any application-specific initialization of the window object.
  351. //
  352. //////////
  353.  
  354. void QTApp_SetupWindowObject (WindowObject theWindowObject)
  355. {
  356. #pragma unused(theWindowObject)
  357.  
  358.     // ***insert application-specific window object configuration here***
  359. }
  360.  
  361.  
  362. //////////
  363. //
  364. // QTApp_RemoveWindowObject
  365. // Do any application-specific clean-up of the window object.
  366. //
  367. //////////
  368.  
  369. void QTApp_RemoveWindowObject (WindowObject theWindowObject)
  370. {
  371. #pragma unused(theWindowObject)
  372.     
  373.     // ***insert application-specific window object clean-up here***
  374.  
  375.     // QTFrame_DestroyMovieWindow in MacFramework.c or QTFrame_MovieWndProc in WinFramework.c
  376.     // releases the window object itself
  377. }
  378.  
  379.  
  380. #if TARGET_OS_MAC
  381. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  382. //
  383. // Apple Event functions.
  384. //
  385. // Use these functions to install handlers for Apple Events and to handle those events.
  386. //
  387. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  388.  
  389. //////////
  390. //
  391. // QTApp_InstallAppleEventHandlers 
  392. // Install handlers for Apple Events.
  393. //
  394. //////////
  395.  
  396. void QTApp_InstallAppleEventHandlers (void)
  397. {
  398.     long        myAttrs;
  399.     OSErr        myErr = noErr;
  400.     
  401.     // see whether the Apple Event Manager is available in the present operating environment;
  402.     // if it is, install handlers for the four required Apple Events
  403.     myErr = Gestalt(gestaltAppleEventsAttr, &myAttrs);
  404.     if (myErr == noErr) {
  405.         if (myAttrs & (1L << gestaltAppleEventsPresent)) {
  406.             // create routine descriptors for the Apple event handlers
  407.             gHandleOpenAppAEUPP = NewAEEventHandlerUPP(QTApp_HandleOpenApplicationAppleEvent);
  408.             gHandleOpenDocAEUPP = NewAEEventHandlerUPP(QTApp_HandleOpenDocumentAppleEvent);
  409.             gHandlePrintDocAEUPP = NewAEEventHandlerUPP(QTApp_HandlePrintDocumentAppleEvent);
  410.             gHandleQuitAppAEUPP = NewAEEventHandlerUPP(QTApp_HandleQuitApplicationAppleEvent);
  411.             
  412.             // install the handlers
  413.             AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, gHandleOpenAppAEUPP, 0L, false);
  414.             AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, gHandleOpenDocAEUPP, 0L, false);
  415.             AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, gHandlePrintDocAEUPP, 0L, false);
  416.             AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, gHandleQuitAppAEUPP, 0L, false);
  417.         }
  418.     }
  419. }
  420.         
  421.         
  422. //////////
  423. //
  424. // QTApp_HandleOpenApplicationAppleEvent 
  425. // Handle the open-application Apple Events.
  426. //
  427. //////////
  428.  
  429. PASCAL_RTN OSErr QTApp_HandleOpenApplicationAppleEvent (const AppleEvent *theMessage, AppleEvent *theReply, unsigned long theRefcon)            
  430. {
  431. #pragma unused(theMessage, theReply, theRefcon)
  432.     
  433.     // we don't need to do anything special when opening the application
  434.     return(noErr);
  435. }
  436.  
  437.  
  438. //////////
  439. //
  440. // QTApp_HandleOpenDocumentAppleEvent 
  441. // Handle the open-document Apple Events. This is based on Inside Macintosh: IAC, pp. 4-15f.
  442. //
  443. // Here we process an Open Documents AE only for files of type MovieFileType.
  444. //
  445. //////////
  446.  
  447. PASCAL_RTN OSErr QTApp_HandleOpenDocumentAppleEvent (const AppleEvent *theMessage, AppleEvent *theReply, unsigned long theRefcon)            
  448. {
  449. #pragma unused(theReply, theRefcon)
  450.  
  451.     long            myIndex;
  452.     long            myItemsInList;
  453.     AEKeyword        myKeyWd;
  454.     AEDescList          myDocList;
  455.     long            myActualSize;
  456.     DescType        myTypeCode;
  457.     FSSpec            myFSSpec;
  458.     OSErr            myIgnoreErr = noErr;
  459.     OSErr            myErr = noErr;
  460.     
  461.     // get the direct parameter and put it into myDocList
  462.     myDocList.dataHandle = NULL;
  463.     myErr = AEGetParamDesc(theMessage, keyDirectObject, typeAEList, &myDocList);
  464.     
  465.     // count the descriptor records in the list
  466.     if (myErr == noErr)
  467.         myErr = AECountItems(&myDocList, &myItemsInList);
  468.     else
  469.         myItemsInList = 0;
  470.     
  471.     // open each specified file
  472.     for (myIndex = 1; myIndex <= myItemsInList; myIndex++)
  473.         if (myErr == noErr) {
  474.             myErr = AEGetNthPtr(&myDocList, myIndex, typeFSS, &myKeyWd, &myTypeCode, (Ptr)&myFSSpec, sizeof(myFSSpec), &myActualSize);
  475.             if (myErr == noErr) {
  476.                 FInfo        myFinderInfo;
  477.             
  478.                 // verify that the file type is MovieFileType; to do this, get the Finder information
  479.                 myErr = FSpGetFInfo(&myFSSpec, &myFinderInfo);    
  480.                 if (myErr == noErr) {
  481.                     if (myFinderInfo.fdType == MovieFileType)
  482.                         // we've got a movie file; just open it
  483.                         OpenMovieInWindow(NULL, &myFSSpec);
  484.                 }
  485.             }
  486.         }
  487.  
  488.     if (myDocList.dataHandle)
  489.         myIgnoreErr = AEDisposeDesc(&myDocList);
  490.     
  491.     // make sure we open the document in the foreground        
  492.     gAppInForeground = true;
  493.     return(myErr);
  494. }
  495.  
  496.  
  497. //////////
  498. //
  499. // QTApp_HandlePrintDocumentAppleEvent 
  500. // Handle the print-document Apple Events.
  501. //
  502. // NOT YET IMPLEMENTED.
  503. //
  504. //////////
  505.  
  506. PASCAL_RTN OSErr QTApp_HandlePrintDocumentAppleEvent (const AppleEvent *theMessage, AppleEvent *theReply, unsigned long theRefcon)            
  507. {
  508. #pragma unused(theMessage, theReply, theRefcon)
  509.  
  510.     return(errAEEventNotHandled);
  511. }
  512.  
  513.  
  514. //////////
  515. //
  516. // QTApp_HandleQuitApplicationAppleEvent 
  517. // Handle the quit-application Apple Events.
  518. //
  519. //////////
  520.  
  521. PASCAL_RTN OSErr QTApp_HandleQuitApplicationAppleEvent (const AppleEvent *theMessage, AppleEvent *theReply, unsigned long theRefcon)            
  522. {
  523. #pragma unused(theMessage, theReply, theRefcon)
  524.  
  525.     // close down the entire framework and application
  526.     QTFrame_QuitFramework();
  527.     return(noErr);
  528. }
  529. #endif // TARGET_OS_MAC
  530.  
  531.  
  532.